package com.lzc.tlmail.tlmailware.listener;

import com.lzc.common.to.LockStockTastTO;
import com.lzc.common.to.mq.Orderto;
import com.lzc.tlmail.tlmailware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 锁定库存监听消息
 * @author lzc
 * @create 2020-10-22 16:19
 */
@Service
@RabbitListener(queues = "stock.release.stock.queue")
public class LockStockListener {

    @Autowired
    private WareSkuService wareSkuService;

    @RabbitHandler
    public void lockOrderStock(Message message, LockStockTastTO lockStockTastTO, Channel channel) throws IOException {
        try {
            wareSkuService.lockOrderStock(lockStockTastTO);
            //这里手动接收消息 删除的意思
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            //异常就拒绝接受 之后重新接收
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    /**
     * 处理关单
     */
    @RabbitHandler
    public void handleOrderCloseRelease(Message message, Orderto orderto, Channel channel)throws IOException {
        try {
            //关单之后 立即通知库存 就是让库存解锁
            wareSkuService.closeOrder(orderto);
            //这里手动接收消息 删除的意思
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            //异常就拒绝接受 之后重新接收
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
